package com.itheima.myreflect2.Student.threaddemo10;

/**
 * @program: Myset
 * @description:
 * @author: xuejingjing
 * @create: 2021-06-19 13:30
 **/
public class MyThread extends Thread {
    private static int ticketCount = 100;
    //（这样保证）锁要是唯一
    private static  Object object = new Object();

    @Override
    public void run() {

        while (true) {
            synchronized (object) {//就是当前的线程对象
                if (ticketCount <= 0) {
                    //卖完了
                    break;
                } else {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    ticketCount--;
                    System.out.println(Thread.currentThread().getName() + "-" + ticketCount + "张票");
                }
            }
        }
    }
}



